<?php
namespace App\Http\Controllers\Quote;

use App\Common\Result;
use App\Http\Controllers\Controller;
use App\Models\Inquiry\InquirySheet;
use App\Models\Quote\Quotation;
use App\Service\Customer\CustomerService;
use App\Service\Inquiry\InquiryService;
use App\Service\Quote\QuoteService;
use Illuminate\Http\Request;

class QuoteController extends Controller
{
    /**
     *  报价单列表
     */
    public function index(Request $request)
    {
        $status = $request->input('status',0);
        $uid = $request->uid;
        if(!$uid){
            Result::msg(-1,'error','用户数据有误');
        }

        $user = CustomerService::findUser([
            'uid' => $uid
        ]);
        if($user->is_salesman != 1){
            Result::msg(-1,'error','您不是销售人员! ');
        }

        switch ($status){
            case 0:
                $data = InquiryService::getAllInquirySheet($status);
                break;
            default:
                $data = QuoteService::quoteList($uid,$status);
        }

        if($data->isEmpty()){
            Result::msg(-1,'error','没有数据了');
        }

        Result::msg(0,'success',$data);
    }

    /**
     *  报价
     */
    public function store(Request $request)
    {
        $inquiry_sheet_id = $request->input('inquiry_sheet_id','');
        if(!$inquiry_sheet_id){
            Result::msg(-1,'error','未找到询价单 ');
        }
        $uid = $request->uid;
        if(!$uid){
            Result::msg(-1,'error','用户数据有误');
        }
        $user = CustomerService::findUser([
            'uid' => $uid
        ]);
        if($user->is_salesman != 1){
            Result::msg(-1,'error','您不是销售人员! ');
        }
        $inquiry_sheet = InquirySheet::where('id',$inquiry_sheet_id)->first();
        if(!$inquiry_sheet){
            Result::msg(-1,'error','未找到询价单 ');
        }
        if($inquiry_sheet->status != 0){
            Result::msg(-1,'error','询价单状态有误 ');
        }

        \DB::beginTransaction();
        try{
            QuoteService::create($request,$inquiry_sheet,$user);
            $inquiry_sheet->status = 1;
            $inquiry_sheet->quote_uid = $user->uid;
            $inquiry_sheet->quote_nickname = $user->nickname;
            $inquiry_sheet->quote_price = $request->price;
            $inquiry_sheet->save();
            $user->quote_num = $user->quote_num+1;
            $user->save();
            \DB::commit();
            Result::msg('0','success',[]);
        }catch (\Exception $e){
            \DB::rollBack();
            Result::msg(-1,'error',$e->getMessage());
        }
    }

    /**
     *  修改报价
     */
    public function update(Request $request)
    {
        $inquiry_sheet_id = $request->input('inquiry_sheet_id','');
        if(!$inquiry_sheet_id){
            Result::msg(-1,'error','未找到询价单 ');
        }
        $uid = $request->uid;
        if(!$uid){
            Result::msg(-1,'error','用户数据有误');
        }
        $user = CustomerService::findUser([
            'uid' => $uid
        ]);
        if($user->is_salesman != 1){
            Result::msg(-1,'error','您不是销售人员! ');
        }
        $inquiry_sheet = InquirySheet::where('id',$inquiry_sheet_id)->first();
        if(!$inquiry_sheet){
            Result::msg(-1,'error','未找到询价单 ');
        }
        if($inquiry_sheet->status != 1){
            Result::msg(-1,'error','询价单状态有误 ');
        }

        $quote_sheet = Quotation::where('inquiry_sheet_id',$inquiry_sheet_id)->first();
        if(!$quote_sheet){
            Result::msg(-1,'error','未找到报价单 ');
        }
        if($quote_sheet->status != 1){
            Result::msg(-1,'error','报价单状态有误 ');
        }
        \DB::beginTransaction();
        try{
            $quote_sheet->price = $request->price;
            $quote_sheet->save();
            $inquiry_sheet->quote_price = $request->price;
            $inquiry_sheet->save();
            \DB::commit();
            Result::msg('0','success',[]);
        }catch (\Exception $e){
            \DB::rollBack();
            Result::msg(-1,'error',$e->getMessage());
        }
    }
}